package com.dfdchem.imd.kaiwu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.manager.GlideManager;
import com.dfdchem.imd.kaiwu.model.Module;

import java.util.List;

public class HomeGridAdapter extends BaseAdapter{

    private Context context;
    private List<Module> modules;

    public HomeGridAdapter(Context context) {
        this.context = context;
    }

    public void setModules(List<Module> modules) {
        this.modules = modules;
    }

    @Override
    public int getCount() {
        return modules == null ? 0 : modules.size();
    }

    @Override
    public Object getItem(int position) {
        return modules == null ? null : modules.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.home_grid, null);
            holder = new ViewHolder();
            holder.imageView = convertView.findViewById(R.id.home_grid_item_image);
            holder.textView = convertView.findViewById(R.id.home_grid_item_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Module module = modules.get(position);
        holder.textView.setText(module.getName());

        new GlideManager(context).load(holder.imageView, module.getIcon(), RequestOptions.bitmapTransform(new RoundedCorners(30)));

        return convertView;
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

}